using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ACME.Northwind.DataTransfer;
using System.Linq.Expressions;
using ACME.Northwind.Service.Interfaces;

namespace ACME.Northwind.Service.IOCExtensions
{
	/// <summary>
	/// This is an extension class for Product DTO objects and lists
	/// </summary>
	public static partial class ProductExtension
	{
		#region RunSelect
		/// <summary>
		/// Select all objects from store.
		/// </summary>
		public static void RunSelect(this List<ProductDTO> list)
		{
			var proxy = ProxyFactory<ProductDTO>.GetProxy();
			list.AddRange(proxy.RunSelect());
		}
		#endregion

		#region RunSelect Paged

		/// <summary>
		/// Using the specified Where expression, execute a query against the database to return a paged result set.
		/// </summary>
		public static PagedQueryResults<ProductDTO> RunSelect(this List<ProductDTO> list, int page, int pageSize, bool ascending, string sortColumn, string linq)
		{
			var proxy = ProxyFactory<ProductDTO>.GetProxy();
			PagedQueryResults<ProductDTO> retval = proxy.RunSelect(page, pageSize, ascending, sortColumn, linq);
			if (retval.GridData != null)
				list.AddRange(retval.GridData);
			return retval;
		}

		#endregion

		#region RunSelect Paged

		/// <summary>
		/// Using the specified Where expression, execute a query against the database to return a result set.
		/// </summary>
		public static void RunSelect(this List<ProductDTO> list, string linq)
		{
			var proxy = ProxyFactory<ProductDTO>.GetProxy();
			list.AddRange(proxy.RunSelect(linq));
		}

		#endregion

		#region Persist

		/// <summary>
		/// Persists this object to store.
		/// </summary>
		public static void Persist(this ProductDTO product)
		{
			var proxy = ProxyFactory<ProductDTO>.GetProxy();
			proxy.Persist(product);
		}

		/// <summary>
		/// Persists this collection to store.
		/// </summary>
		public static void Persist(this List<ProductDTO> productList)
		{
			var proxy = ProxyFactory<ProductDTO>.GetProxy();
			proxy.Persist(productList);
		}

		#endregion

		#region Product SelectUsingPK

		/// <summary>
		/// Select a single object from this collection by its primary key.
		/// </summary>
		public static bool SelectByPrimaryKey(this ProductDTO product, int productid)
		{
			var proxy = ProxyFactory<ProductDTO>.GetProxy();
			IProductService productService = (IProductService)proxy;
			var item = productService.SelectByPrimaryKey(productid);
			if (item == null) return false;
			CopyObject(item, product);
			return true;
		}

		#endregion

		#region Product Select By Foreign Key
		/// <summary>
		/// Get the related 'OrderDetail' DTO list
		/// </summary>
		public static OrderDetailDTO[] GetOrderDetailList(this ProductDTO item)
		{
			var proxy = ProxyFactory<ProductDTO>.GetProxy();
			IProductService service = (IProductService)proxy;
			return service.GetOrderDetailList(item).ToArray();
		}

		/// <summary>
		/// Get the related 'Category' DTO object
		/// </summary>
		public static CategoryDTO GetCategory(this ProductDTO item)
		{
			var proxy = ProxyFactory<ProductDTO>.GetProxy();
			IProductService service = (IProductService)proxy;
			return service.GetCategoryItem(item);
		}

		/// <summary>
		/// Get the related 'Supplier' DTO object
		/// </summary>
		public static SupplierDTO GetSupplier(this ProductDTO item)
		{
			var proxy = ProxyFactory<ProductDTO>.GetProxy();
			IProductService service = (IProductService)proxy;
			return service.GetSupplierItem(item);
		}


		#endregion

		#region Delete

		/// <summary>
		/// Delete a single object.
		/// </summary>
		public static void Delete(this ProductDTO product)
		{
			var proxy = ProxyFactory<ProductDTO>.GetProxy();
			IProductService productService = (IProductService)proxy;
			productService.Delete(product);
		}

		#endregion

		/// <summary>
		/// Clones one DTO to another.
		/// </summary>
		internal static void CopyObject(ProductDTO source, ProductDTO destination)
		{
			destination.CategoryId = source.CategoryId;
			destination.UnitsOnOrder = source.UnitsOnOrder;
			destination.UnitsInStock = source.UnitsInStock;
			destination.UnitPrice = source.UnitPrice;
			destination.SupplierId = source.SupplierId;
			destination.ReorderLevel = source.ReorderLevel;
			destination.QuantityPerUnit = source.QuantityPerUnit;
			destination.ProductName = source.ProductName;
			destination.ProductId = source.ProductId;
			destination.Discontinued = source.Discontinued;
		}

	}
}
